<!--我的钱包-->
<template>
  <div class="all bg1" @viewappear="viewappear" @viewdisappear="viewdisappear">

    <template  v-if="account">

      <!--总资产-->
      <div class="assets bg2">
        <LImage class="texture3" :src="Config.img('texture3.png')"></LImage>

        <text class="font28 text2">{{T('总资产折合')+'('+unit+')'}}</text>

        <div class="height20"></div>
        <div class="row aend">
          <text class="font48 text2">{{totalModel}}</text>
          <text class="font30 text3 text-up8">{{" ≈"+totalBTC+' BTC'}}</text>
        </div>
      </div>
      
      <!--账户-->
      <div>
        <scroller class="scroller" scroll-direction="horizontal" show-scrollbar=false>
          <div class="wrapper">
            <div v-for="(item, index) in accounts" v-bind:key="('account'+index)" 
             ref="cell" @click="accountClick($event, index)">
              <div :class="['account', type == index  ? 'main1': 'bg4']">
                <text class="font28 text2">{{item.name}}</text>
                <div class="all"></div>
                <text class="font42 text1">{{item.totalModel+" "+unit}}</text>
                <div class="height6"></div>
                <text class="font24 text2">{{"≈ "+item.totalBTC+' BTC'}}</text>
              </div>
            </div>
          </div>
        </scroller>
        <div class="tag-bg">
          <div :class="['tag', type == index ? 'main1': 'bg4']" v-for="(item, index) in accounts" v-bind:key="('tag'+index)"></div>
        </div>
      </div>

      <!--资产列表-->
      <div class="title ">
        <VTitle :title="T('资产明细')"></VTitle>
        <text class="font26 main-t1" v-if="type == 1" @click="frozenClick">{{T('保证金冻结')+' >'}}</text>
      </div>
      <list class="list">
        <cell v-for="(item, index) in account.assest" v-bind:key="('cell'+index)">
          <VCoin :item="item" :unit="unit" @click="coinClick(item)"></VCoin>
        </cell>
      </list>

      <!--消费账户按-->
      <div class="menu1" v-if="type == 0">
        <div class="but bt4" @click="buyVtnClick">
          <text class="font30 text2">{{T('购买VTN')}}</text>
        </div>
        <div class="width60"></div>
        <div class="but bt4" @click="transferClick" >
          <text class="font30 text2">{{T('内部转账')}}</text>
        </div>
      </div>

      <!--保证金账户按钮-->
      <div class="menu2" v-else-if="type == 1">
        <div class="but bt4" @click="buyClick">
          <text class="font30 text2">{{T('充值')}}</text>
        </div>
        <div class="but bt4" @click="huaZhuanClick('保证金账户')">
          <text class="font30 text2">{{T('划转')}}</text>
        </div>
        <div class="but bt4" @click="withdrawalClick">
          <text class="font30 text2">{{T('提现')}}</text>
        </div>
      </div>

      <!--收益账户按钮-->
      <template v-else>

        <div class="menu2" v-if="funMap.incomeWallet_recharge">
          <div class="but bt4" @click="huaZhuanClick('收益账户')">
            <text class="font30 text2">{{T('划转')}}</text>
          </div>
          <div class="but bt4" @click="exchangeClick">
            <text class="font30 text2">{{T('兑换')}}</text>
          </div>
          <div class="but bt4" @click="withdrawalClick">
            <text class="font30 text2">{{T('提现')}}</text>
          </div>
        </div>
        <template v-else>
          <div class="menu2">
            <div class="but-min bt4" @click="exchangeClick">
              <text class="font30 text2">{{T('兑换')}}</text>
            </div>
            <div class="but-min bt4" @click="buyClick">
              <text class="font30 text2">{{T('充值')}}</text>
            </div>
            <div class="but-min bt4" @click="huaZhuanClick('收益账户')">
              <text class="font30 text2">{{T('划转')}}</text>
            </div>
            <div class="but-min bt4" @click="withdrawalClick">
              <text class="font30 text2">{{T('提现')}}</text>
            </div>
          </div>
        </template>
      </template>
    </template>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
/***************** 总资产*/
.assets {
  padding-top: 40px;
  padding-left: 40px;
  padding-right: 40px;
  padding-bottom: 45px;
}
.text-up8{
  margin-bottom: 8px;
}

/***************** 账户列表*/
.scroller{
  flex-direction: row;
  margin-top: 20;
}
.wrapper{
  flex-direction: row;
  padding-left: 20px;
  padding-right: 30px;
}
.account{
  flex-direction: column;
  height: 205px;
  width:440px;
  border-radius: 24px;
  padding: 30px;
  margin-left: 10px;
}
.tag-bg{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.tag{
  width:10px;
  height:10px;
  border-radius:50%;
  margin-left: 15px;
  margin-right: 15px;
}

/***************** 资产列表*/
.title {
  flex-direction: row;
  justify-content: space-between;
  padding-left: 40px;
  padding-right: 40px;
}
.list {
  flex: 1;
}

/***************** 菜单*/

.menu1{
  flex-direction: row;
  justify-content: center;
  padding-bottom: 60px;
}
.menu2{
  flex-direction: row;
  justify-content: space-around;

  padding-left: 30px;
  padding-right: 30px;
  padding-bottom: 60px;
}
.menu3{
  flex-direction: row;

  padding-left: 30px;
  padding-right: 30px;
  padding-bottom: 60px;
}
.but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width:218px;
  height:80px;
  border-radius:12px;
}
.but-min{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width:160px;
  height:80px;
  border-radius:12px;
}
.width60{
  width: 60px;
}


/***************** 间距*/
.height6 {
  height: 6px;
}
</style>

<script>

//组件
import VTitle from '@/wallet/view/VTitle.vue';  //标题
import VCoin from '@/wallet/view/VCoin.vue'  //收益账户

//功能
import jnavigator from "@/_js/navigator";
import jconfig from "@/_js/config";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jhttps from "@/_js/https";
import jdebug from "@/_js/debug";

const weexFunction = weex.requireModule("WeexFunction");
const dom = weex.requireModule('dom');

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('我的钱包'));

export default {
  
  components: {
    VTitle,
    VCoin,
  },

  data() {
    return {
      T:T,
      Config:jconfig,

      type:0, //0消费账户 1保证金 2收益账户


      unit:"", // 计价单位值
      totalModel:"0", //总资产
      totalBTC:"0", //总资产BTC
      accounts:[], //账户表
      account:undefined, //账户
      funMap: {}, //功能现隐对象
    };
  },

  created() {
    var param = weex.config;
    this.type = param.type == undefined ? this.type : param.type;

    //计价单位
    weexFunction.getConfig("current_bz",(value)=>{
      this.unit = value == "USD"?"USD":"CNY";
      jmodal.showLoad();
      this.httpsAssestHome();
      this.httpsHidePage();
    });
  },


  computed: {
    
    //true 显示充值
    isCZ() {
      
      var bol = false;
      this.account.assest.forEach((item)=>{
        bol = bol || (item.coinName=='VT');
      });

      return bol;
    }
  },


  methods: {

    initData(data) {
      
      var accounts = [];
      var totalModel = "0";
      var totalBTC = "0";

      //统计
      var statistics = (name, type, coins, wallet)=>{

        var assest = this.getAssest(name, coins, data.btcUsdt, data.cnyUsdt);
        assest.type = type;
        assest.wallet = wallet;
        accounts.push(assest);

        totalModel = jmath.jia(totalModel, assest.totalModel);
        totalBTC = jmath.jia(totalBTC, assest.totalBTC);
      };



      //消费账户
      if(data.consumeBalnace){

        //2021.3.8日 隐藏消费钱包USDT资产
        var coins = [];
        for (let index = 0; index < data.consumeBalnace.length; index++) {
          var coin = data.consumeBalnace[index];
          if (coin.coinName != 'USDT') {
            coins.push(coin);
          }
        }
        statistics(T('消费账户'), 1, coins, 'consumeWallet');
      }
      
      //保证金账户
      if (data.depositBalance) {
        statistics(T('保证金账户'), 0, data.depositBalance, 'depositWallet');
      }
      
      //收益账户
      if(data.incomeBalnace){
        statistics(T('收益账户'), 2, data.incomeBalnace, 'incomeWallet');
      }

      this.totalModel = jmath.gfMoney(totalModel, false);
      this.totalBTC = jmath.gfCoin(totalBTC, 'BTC', false);
      return accounts;
    },

    /*******************************通信触发*/
    //完全显示
    viewappear() {
      weexFunction.logPageView('钱包', true);
      if (this.unit) {
        this.httpsAssestHome();
        this.httpsHidePage();
      }
    },

    //完全隐藏
    viewdisappear() {
      weexFunction.logPageView('钱包', false);
    },

    /******************************* HTTP*/
    //https钱包首页
    httpsAssestHome(){

      var param = {'type':"getCash"};
      jhttps.post("wallet/walletPage", param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          data = data.data;
          this.accounts = this.initData(data);
          this.account = this.accounts[this.type];
          if (this.type > 0 && this.$refs.cell) {
            var cell = this.$refs.cell[this.type];
            dom.scrollToElement(cell, {offset:-30});
          }
        }else{
          jmodal.toast(data.msg);
        }
      });
    },
    

    //https功能隐藏判断
    httpsHidePage(){

      var param = {};
      jhttps.post("systemV2/hidePage", param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          this.funMap = data.data;
        } else {
          jmodal.toast(data.msg);
        }
      });
    },
    
    /******************************* 其他*/
    /** 获取资产模型
     * @param name 资产名称
     * @param coins 资产数组
     * @param btcUsdt btc对usdt行情价
     * @param cnyUsdt cny对usdt行情价
     */
    getAssest(name, coins, btcUsdt, cnyUsdt){

      var totalModel = "0";
      var totalUSD = "0";
      for (let index = 0; index < coins.length; index++) {
        
        var coin = coins[index];

        var usd = jmath.cheng(coin.coinNum, coin.rate);
        var model = this.unit == "CNY" ? jmath.cheng(usd, cnyUsdt) : usd;
        coin.model = jmath.gfMoney(model, false); //折合钱-CNY、USD

        totalModel = jmath.jia(totalModel, coin.model); //总折合
        totalUSD = jmath.jia(totalUSD, usd); //总USD
      }
      var totalBTC = jmath.chu(totalUSD, btcUsdt); //总BTC折合
      totalBTC = jmath.gfCoin(totalBTC, 'BTC', false);

      var coin = {};
      coin.name = name;
      coin.assest = coins;
      coin.totalModel = totalModel; //账户总折合钱
      coin.totalBTC = totalBTC; //账户总折合BTC
      return coin;
    },

    /******************************* 点击*/
    //保证金冻结
    frozenClick(){
      var nav = {'navColor':'#151a2a'};
      jnavigator.open("", "wallet/Ensure", undefined, nav);
    },

    //账户
    accountClick(event, index){
      const target = event.target;
      dom.scrollToElement(target, {offset: -30});
      this.account = this.accounts[index];
      this.type = index;
    },

    //币点击
    coinClick(item){
      var nav = {'navColor':'#151a2a'};
      var param = {'unit':this.unit,
                  'coinType':item.coinName,
                  'wallet':this.account.wallet};
      jnavigator.open("", "wallet/WalletCoin", param, nav);
    },

    //购买VTN
    buyVtnClick(){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':this.account.wallet};
      jnavigator.open("", "wallet/WalletBuy", param, nav);
    },

    //内部转账
    transferClick(){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':this.account.wallet};
      jnavigator.open("", "wallet/WalletZz", param, nav);
    },

    //充值保证金
    buyClick(){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':this.account.wallet};
      jnavigator.open("", "wallet/EnsureBuy", param, nav);
    },

    //提现
    withdrawalClick(){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':this.account.wallet};
      jnavigator.open("", "wallet/WalletTx", param, nav);
    },

    //划转
    huaZhuanClick(title){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'fromAccount':title};
      jnavigator.open("", "wallet/WalletHz", param, nav);
    },

    //兑换
    exchangeClick(){
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':this.account.wallet};
      jnavigator.open("", "wallet/WalletDh", param, nav);
    },
  }
}
</script>

